# -*- coding: utf-8 -*-
# @Time : 2022/4/1 14:05
# @FileName: GetPackage.py
# @Mail: lizhigen1996@aliyun.com

__author__ = 'Zhigen.li'


from urllib import request, error
import sys


def progressbar(cur, total=100):
    percent = '{:.2%}'.format(cur / total)
    sys.stdout.write("正在更新 [%-100s] %s\r" % ('=' * int(cur), percent))
    sys.stdout.flush()


def schedule(blocknum, blocksize, totalsize):
    """
    blocknum:当前已经下载的块
    blocksize:每次传输的块大小
    totalsize:网页文件总大小
    """
    if totalsize == 0:
        percent = 0
    else:
        percent = blocknum * blocksize / totalsize
    if percent > 1.0:
        percent = 1.0
    percent = percent * 100
    progressbar(percent)


if __name__ == "__main__":
    url = 'http://qiniu.zhigen.cool/Fund.zip'
    filepath = r'C:\Users\Zhigen\Desktop\Fund.zip'

    print('基金有风险，投资需谨慎，您的一切操作与本软件无关，请谨慎操作\n'
          '本软件获取的所有基金数据均为使用者自行获取，任何后果与软件开发者无关\n'
          '本软件作者未授权任何个人或组织使用本软件内数据获取利益，任何后果与软件开发者无关\n'
          '当您使用本软件起，即默认您已同意承担所有投资风险，任何后果与软件开发者无关\n'
          '最终解释权归软件开发者所有！')

    try:
        request.urlretrieve(url, filepath, schedule)
    except error.HTTPError as e:
        print(e)
        print('\r\n' + '更新失败!' + '\r\n')
    else:
        print('\r\n' + '更新成功!')
